//
//  BVVideoPlayView.m
//  BVUIBizKit
//
//  Created by 崔晋 on 2019/10/19.
//

#import "BVVideoPlayView.h"

#import "Masonry.h"


@interface BVViderToolView ()

@property (nonatomic, strong) UIButton *fullScreenButton;

@end

@implementation BVViderToolView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.25];
    [self addSubview:self.fullScreenButton];
    [self.fullScreenButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.mas_centerY);
        make.right.mas_equalTo(self.mas_right).offset(-15);
    }];
}

- (UIButton *)fullScreenButton {
    if (!_fullScreenButton) {
        _fullScreenButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_fullScreenButton setImage:[UIImage bv_imageNamed:@"full_screen_button" bundle:[BVBundle bundleWithClass:self.class name:BV_BUNDLE_HOME_SCENE_MODULE]] forState:UIControlStateNormal];
    }
    return _fullScreenButton;
}


@end



@interface BVVideoPlayView ()

@property (nonatomic, strong) BVViderToolView *toolView;

@end

@implementation BVVideoPlayView

- (void)layoutSubviews {
    [super layoutSubviews];
    if (self.subviews.lastObject != self.toolView) {
        [self bringSubviewToFront:self.toolView];
    }
}

+ (instancetype)sharedInstance {
    static BVVideoPlayView *view = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        view = [[BVVideoPlayView alloc] init];
    });
    return view;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
        [self addSubview:self.toolView];
        [self.toolView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.and.bottom.equalTo(self);
            make.height.mas_equalTo(50);
        }];
        UITapGestureRecognizer *tapG = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
        [self addGestureRecognizer:tapG];
    }
    return self;
}

- (void)fullScreen {
    if (self.fullScreenBlock) {
        self.fullScreenBlock();
    }
}

- (void)tapGesture:(UITapGestureRecognizer *)sender {
    if (self.playController.isPlaying) {
        [self.playController pause];
    }else {
        [self.playController play];
    }
}

- (CGSize)videoSize {
    CGFloat width = [[self.playController valueForKey:@"videoWidth"] doubleValue];
    CGFloat height = [[self.playController valueForKey:@"videoHeight"] doubleValue];
    return CGSizeMake(width, height);
}

- (void)setUrl:(NSString *)url {
    _url = [url copy];
    if (self.playController) {
        [self.playController.view removeFromSuperview];
        [self.playController stop];
    }
    if (url == nil) {
        return;
    }
    IJKFFOptions *options = [IJKFFOptions  optionsByDefault];
    _playController = [[IJKFFMoviePlayerController alloc] initWithContentURLString:url withOptions:options];
    [self addSubview:self.playController.view];
    [self.playController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    self.playController.scalingMode = IJKMPMovieScalingModeAspectFit;
}

- (BVViderToolView *)toolView {
    if (!_toolView) {
        _toolView = [[BVViderToolView alloc] init];
        [_toolView.fullScreenButton addTarget:self action:@selector(fullScreen) forControlEvents:UIControlEventTouchUpInside];
    }
    return _toolView;
}


@end
